﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using IHISCETeim.Library;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using IHISCETeim.Model;
using System.Data.SqlTypes;


namespace IHISCETeim.DAL
{
    public class OJobDAL
    {
        public static int Add(OJobInfo obj)
        {
            Database DB = DatabaseFactory.CreateDatabase();
            //obj.DownlineDate = obj.DownlineDate == System.DateTime.MinValue ? null : obj.DownlineDate;
            DbCommand dbCommand = DB.GetStoredProcCommand("OJob_Add",obj.MachineType,obj.SerialNumber.Trim().ToUpper(),obj.EngineNumber,obj.DownlineDate,obj.GPSIsSetUp,obj.InStorageDate,obj.GPSNumber,obj.OnlineDate,obj.GPSOutStorage,obj.MACStock,obj.GPSStock,obj.OutStockType,obj.AgentName,obj.Remark,obj.ConfirmMsg);
            try
            {
                 return Convert.ToInt32( DB.ExecuteScalar(dbCommand));
                 
            }
            catch {
                return 0;
            }
        }
        public static bool Delete(string SerialNumber)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("OJob_Delete", SerialNumber.Trim().ToUpper());
            try
            {
                DB.ExecuteNonQuery(dbCommand);
                return true;
            }
            catch
            {
                return false;
            }
        }
        public static IDataReader GetBySerialNumber(string SerialNumber)
	    {
		    Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("OJob_GetBySerialNumber", SerialNumber.Trim().ToUpper());
            return DB.ExecuteReader(dbCommand);
	    }

        public static DataTableCollection GetPagination(string WhereSql, int PageIndex, int PageSize, string Sort, ref int TotalCount)
	    {
		    Database DB = DatabaseFactory.CreateDatabase();		
		
            DbCommand dbCommand = DB.GetStoredProcCommand("OJob_GetPagination");
            DB.AddInParameter(dbCommand, "WhereSql", DbType.String,WhereSql);
            DB.AddInParameter(dbCommand, "PageIndex", DbType.Int32,PageIndex);
            DB.AddInParameter(dbCommand, "PageSize", DbType.Int32,PageSize);
            DB.AddInParameter(dbCommand, "Sort", DbType.String,Sort);
            DB.AddOutParameter(dbCommand, "TotalCount", DbType.Int32,8);
			DataTableCollection dt=  DB.ExecuteDataSet(dbCommand).Tables;
				
            TotalCount=Convert.ToInt32( DB.GetParameterValue(dbCommand,"TotalCount"));
            return dt;
	    }
        public static bool Update(OJobInfo obj)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("OJob_Update", obj.JobId, obj.MachineType, obj.SerialNumber.Trim().ToUpper(), obj.EngineNumber, obj.DownlineDate, obj.GPSIsSetUp, obj.InStorageDate, obj.GPSNumber, obj.OnlineDate, obj.GPSOutStorage, obj.MACStock, obj.GPSStock, obj.OutStockType, obj.AgentName, obj.Remark, obj.ConfirmMsg,obj.IsSellStorage);
            try
            {
                DB.ExecuteNonQuery(dbCommand);
                return true;
            }
            catch (Exception exc)
            {
                return false;
            }
        }

        public static bool UpdateIsSellStorage(int JobId, bool IsSellStorage)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("OJob_UpdateIsSellStorage", JobId, IsSellStorage);
            try
            {
                DB.ExecuteNonQuery(dbCommand);
                return true;
            }
            catch (Exception exc)
            {
                return false;
            }
        }

        public static IDataReader GetALLJobContent()
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("OJobContent_GetAll");
            return DB.ExecuteReader(dbCommand);
        }
        public static DataTable GetALLJobContent2()
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("OJobContent_GetAll");
            return DB.ExecuteDataSet(dbCommand).Tables[0];
        }
        public static object GetReport(string mactype, string ItemType, string StateDate, string EndDate,string color)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("OJob_GetReport", mactype.ToUpper().Trim(), ItemType, StateDate, EndDate, color);
            return DB.ExecuteScalar(dbCommand);
        }

        public static object GetCountByFiled(string FiledName)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("OJob_GetCountByFiled",FiledName.Trim());
            return DB.ExecuteScalar(dbCommand);
        }


        /// <summary>
        /// 保存目标
        /// </summary>
        /// <param name="JobGoalKey"></param>
        /// <param name="JobGoalValue"></param>
        public static void OJobGoal_Edit(string JobGoalKey, string JobGoalValue)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("OJobGoal_Edit", JobGoalKey.Trim(), JobGoalValue.Trim());
            DB.ExecuteNonQuery(dbCommand);
        }
        /// <summary>
        /// 获取目标
        /// </summary>
        /// <param name="JobGoalKey"></param>
        /// <returns></returns>
        public static object OJobGoal_Get(string JobGoalKey)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("OJobGoal_Get", JobGoalKey.Trim());
            return  DB.ExecuteScalar(dbCommand);
        }
        /// <summary>
        /// 验证机号对应的发动机号是否一致 返回空为一致，否则返回不一致信息
		///@VTtype=1 来源是机器一览表
		///@vtype=2 来源是完成检查书
		///@vtype=3 来源是完检单
        /// </summary>
        /// <param name="SerialNumber"></param>
        /// <param name="EngineNumber"></param>
        /// <param name="VTtype"></param>
        /// <returns></returns>
        public static string CheckEngineNumber(string SerialNumber, string EngineNumber, int VTtype)
        {
            Database DB = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = DB.GetStoredProcCommand("OJob_CheckEngineNumber", SerialNumber, EngineNumber, VTtype);
            return DB.ExecuteScalar(dbCommand).ToString();
        }
    }
}
